<?php
namespace app\api\controller\datascreen;

use app\api\controller\Base;
use app\common\model\company\Company;
use app\common\model\orderhawa\Hawa;
use app\common\model\orderhawa\HawaList;
use app\common\model\orderreceipt\Receipt;
use app\common\model\orderreceipt\ReceiptList;
use app\api\controller\mytool\Setting;
use think\facade\Request;
use Firebase\JWT\JWT;

class Index extends Base
{
    /**
     * 产生jwt加密串，参考session
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 统计联单量
     * @return [type] [description]
     */
    public function sumcount()
    {
        self::is_login();
        $thisyear    = date('Y');
        $thismonth   = date('m');
        $thisday     = date('d');
        $thisweekday = date('w');
        $list        = ['all', 'year', 'month', 'today'];
        $rdata       = [];
        foreach ($list as $lk => $lv) {
            $hawalist    = HawaList::where('State', HawaList::STATEOK);
            $receiptlist = ReceiptList::where('State', ReceiptList::STATEOK);
            switch ($lv) {
                case 'today':
                    //今天
                    $begin = mktime(0, 0, 0, $thismonth, $thisday, $thisyear);
                    $end   = mktime(23, 59, 59, $thismonth, $thisday, $thisyear);
                    break;
                case 'month':
                    //php获取本月起始时间戳和结束时间戳
                    $begin = mktime(0, 0, 0, $thismonth, 1, $thisyear);
                    $end   = mktime(23, 59, 59, $thismonth, date('t'), $thisyear);
                    break;
                case 'year':
                    //php获取本年起始时间戳和结束时间戳
                    $begin = mktime(0, 0, 0, 1, 1, $thisyear);
                    $end   = mktime(23, 59, 59, 12, 31, $thisyear);
                    break;
            }
            if ($lv !== 'all') {
                $hawalist    = $hawalist->where('TransitEndTime', '>', date('Y-m-d H:i:s', $begin))->where('TransitEndTime', '<', date('Y-m-d H:i:s', $end));
                $receiptlist = $receiptlist->where('BussinessTime', '>', date('Y-m-d H:i:s', $begin))->where('BussinessTime', '<', date('Y-m-d H:i:s', $end));
            }
            $company = $hawalist;
            //订单个数
            $rdata[$lv]['count'] = $hawalist->count();
            //产废单位count
            $rdata[$lv]['company'] = $company->distinct(true)->count('CompanyId');
            //接受量cout
            $receiptlistid = $receiptlist->column('ReceiptListId');
            if (!empty($receiptlistid)) {
                $rdata[$lv]['weight'] = Receipt::where('ReceiptListId', 'in', $receiptlistid)->sum('Weight').'';
            } else {
                $rdata[$lv]['weight'] = 0;
            }
        }
        return myok($rdata);
    }

    /**
     * 获取当前车载情况
     * @return [type] [description]
     */
    public function cardata()
    {
        self::is_login();
        //车上的订单
        $orderdata = HawaList::where('State', HawaList::STATETRANSPORT);
        //运货中的车辆
        $rdata['carcount'] = count($orderdata->distinct(true)->column('TranSportId'));
        //运输中的重量
        $hawalistid      = $orderdata->column('HawaListId');
        $rdata['weight'] = Hawa::where('HawaListId', 'in', $hawalistid)->sum('Weight');
        //运输中的订单数
        $rdata['count'] = count($hawalistid);
        //在车上的订单
        $rdata['listdata'] = $orderdata->with('companyinfo')->withSum('listinfo', 'Weight')->visible(['HawaListId', 'State', 'UpTime', 'OrderNumber', 'ConfirmState', 'companyinfo.Name', 'companyinfo.CompanyId', 'companyinfo.Lat', 'companyinfo.Lon', 'companyinfo.Tel', 'TransitCompanyId', 'TransitCompanyName', 'TransitEscortName', 'TransitCompanyAddress', 'TransitNumber', 'TransitTel', 'TranSportId', 'TranSportType', 'TransitBrandNumber', 'TransitDriverId', 'TransitEscortId', 'TransitDrvierName', 'TransitDrvierTel', 'TransitUser', 'TransitEndPoint', 'TransitStartTime', 'TransitStartingPoint', 'TransitViaLand', 'listinfo_sum'])->select();
        foreach ($rdata['listdata'] as $key => $value) {
            $rdata['listdata'][$key]['State'] = HawaList::getStatusText($value['State']);
        }
        return myok($rdata);
    }

    /**
     * 获取订单详情
     * @return [type] [description]
     */
    public function getorderinfo()
    {
        self::is_login();
        $id = input('id');
        if (empty($id)) {return myerr('缺少订单id');}
        $data = HawaList::with('bussinfo,userbussinfo,tranuserinfo')->visible(['tranuserinfo' => ['Name', 'Iphone'], 'bussinfo' => ['Name', 'Tel'], 'userbussinfo' => ['Name', 'Iphone']])->get($id);
        if (empty($data)) {return myerr('缺少订单信息');}
        $data['list']      = Hawa::where('HawaListId', $id)->with('goodinfo')->visible(['Weight', 'ObjectState', 'GoodName', 'Box', 'BoxCount', 'goodinfo' => ['Type', 'OrderNumber', 'IndustrySource', 'Name']])->select();
        $data['SumWeight'] = 0;
        foreach ($data['list'] as $key => $value) {
            $data['SumWeight'] = $data['SumWeight'] + $value['Weight'];
        }
        $data['company'] = Company::user_getinfo($data['CompanyId']);
        $data['State']   = HawaList::getStatusText($data['State']);
        return myok($data);
    }

    /**
     * 获取订单性情
     * @return [type] [description]
     */
    public function getorderlist()
    {
        self::is_login();
        $car    = input('CarNumber');
        $number = input('number');
        $star   = input('start');
        $end    = input('end');
        $limit  = 30;
        $pages  = empty(input('pages')) ? 0 : input('pages') - 1;

        $data = HawaList::where('State', '<>', HawaList::STATEDELETE);
        if (!empty($car)) {
            $data = $data->where('TransitBrandNumber', 'like', '%' . $car . '%');
        }

        if (!empty($number)) {
            $data = $data->where('OrderNumber', 'like', '%' . $number . '%');
        }

        if (!empty($star)) {
            $data = $data->where('UpTime', '>=', $star);
        }

        if (!empty($end)) {
            $data = $data->where('UpTime', '<=', $end);
        }
        //总条数
        if (empty($rdata['count'])) {
            $rdata['count'] = $data->count();
        }

        $data = $data->with('companyinfo')->withSum('listinfo', 'Weight')->limit($pages * $limit, $limit)->order('UpTime desc')->visible(['HawaListId', 'State', 'UpTime', 'OrderNumber', 'ConfirmState', 'companyinfo.Name', 'companyinfo.CompanyId', 'companyinfo.Lat', 'companyinfo.Lon', 'companyinfo.Tel', 'TransitCompanyId', 'TransitCompanyName', 'TransitEscortName', 'TransitCompanyAddress', 'TransitNumber', 'TransitTel', 'TranSportId', 'TranSportType', 'TransitBrandNumber', 'TransitDriverId', 'TransitEscortId', 'TransitDrvierName', 'TransitDrvierTel', 'TransitUser', 'TransitEndPoint', 'TransitStartTime', 'TransitStartingPoint', 'TransitViaLand', 'listinfo_sum', 'TransitEndTime'])->select();

        foreach ($data as $key => $value) {
            $data[$key]['State'] = HawaList::getStatusText($value['State']);
        }
        $rdata['list'] = $data;

        return myok($rdata);
    }

     /**
     * 大屏登陆逻辑
     * @return boolean [description]
     */
    public static function login()
    {
        $p  = input('screen_password');
        $sb = Setting::login_screen_password($p);
        if (!$sb) {
            moerr('密码错误，请重新尝试');
        }else{
        	return myok('登陆成功');
        }
    }

    /**
     * 大屏检测登陆逻辑
     * @return boolean [description]
     */
    public static function is_login()
    {
        $p  = Request::header('token');
        if(empty($p)){
        	$p = '';
        }
        $sb = Setting::login_screen_password($p);
        if (!$sb) {
            moerr('请输入密码', '', 'relogin');
        }
    }
}
